package kau.dao;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import kau.common.DBUtil;
import kau.exception.DuplicationException;
import kau.exception.RecordNotFoundException;
import kau.vo.Writing;

import org.apache.ibatis.session.SqlSession;

public class WritingDAOImpl implements WritingDAOIF {

	DBUtil db = null;
	private static WritingDAOImpl instance;

	private WritingDAOImpl() {
		db = DBUtil.getInstance();
	}

	public static WritingDAOImpl getInstance() {
		if (instance == null)
			instance = new WritingDAOImpl();
		return instance;
	}

	private boolean isExist(int wNumber) {
		SqlSession session = db.getSession();
		boolean flag = false;
		String result = null;
		try {
			result = session.selectOne("dbConfig.WritingMapper.isExist",
					wNumber);

		} finally {
			if (session != null)
				session.close();
		}
		if (result != null)
			flag = true;

		return flag;

	}

	@Override
	public void insert(Writing writing) throws DuplicationException {
		if (isExist(writing.getwNumber()))
			throw new DuplicationException(writing.getwNumber());
		SqlSession session = db.getSession();
		try {
			session.insert("dbConfig.WritingMapper.insert", writing);
			session.commit();
		} finally {
			if (session != null)
				session.close();
		}
	}

	@Override
	public void update(Writing writing) throws RecordNotFoundException {
		if (!isExist(writing.getwNumber()))
			throw new RecordNotFoundException(writing.getwNumber());
		SqlSession session = db.getSession();
		try {
			session.update("dbConfig.WritingMapper.update", writing);
			session.commit();
		} finally {
			if (session != null)
				session.close();
		}
	}

	@Override
	public void delete(int wNumber) throws RecordNotFoundException {
		if (!isExist(wNumber))
			throw new RecordNotFoundException(wNumber);

		SqlSession session = db.getSession();
		
		
		try {
			session.delete("dbConfig.WritingMapper.delete", wNumber);
			session.commit();
		} finally {
			if (session != null)
				session.close();
		}
	}

	@Override
	public Writing getWriting(int wNumber) throws RecordNotFoundException {
		if (!isExist(wNumber))
			throw new RecordNotFoundException(wNumber);
		SqlSession session = db.getSession();
		Writing writing_ret = null;
		//System.out.println("getWriting: "+wNumber);
		try {
			writing_ret = session.selectOne(
					"dbConfig.WritingMapper.getWriting", wNumber);
			session.commit();
		} finally {
			if (session != null)
				session.close();
		}
		//System.out.println(writing_ret.toString());

		return writing_ret;
	}

	@Override
	public ArrayList<Writing> getAllWritings(int bNumber) {
		ArrayList<Writing> writingList = new ArrayList<Writing>();
		SqlSession session = db.getSession();
		try {
			writingList = (ArrayList) session.selectList(
					"dbConfig.WritingMapper.getAllWritings", bNumber);
		} finally {
			if (session != null)
				session.close();
		}

		return writingList;
	}

	@Override
	public String download(int wNumber) throws RecordNotFoundException {
		if (!isExist(wNumber))
			throw new RecordNotFoundException(wNumber);
		SqlSession session = db.getSession();
		String filePath = null;
		try {
			filePath = session.selectOne("dbConfig.WritingMapper.download",
					wNumber);
			session.commit();
		} finally {
			if (session != null)
				session.close();
		}
		return filePath;
	}

	@Override
	public ArrayList<Writing> custNameSearch(String keyword,int bNumber) {
		ArrayList<Writing> writingList = new ArrayList<Writing>();
		SqlSession session = db.getSession();
		HashMap param = new HashMap();
		param.put("keyword", keyword);
		param.put("bNumber", bNumber);
		try {
			writingList = (ArrayList) session.selectList(
					"dbConfig.WritingMapper.custNameSearch", param);
		} finally {
			if (session != null)
				session.close();
		}

		return writingList;
	}

	@Override
	public ArrayList<Writing> titleSearch(String title,int bNumber) {
		ArrayList<Writing> writingList = new ArrayList<Writing>();
		SqlSession session = db.getSession();
		HashMap param = new HashMap();
		param.put("keyword", title);
		param.put("bNumber", bNumber);
		try {
			writingList = (ArrayList) session.selectList(
					"dbConfig.WritingMapper.titleSearch", param);
		} finally {
			if (session != null)
				session.close();
		}

		return writingList;
	}

	@Override
	public ArrayList<Writing> contentSearch(String content,int bNumber) {
		ArrayList<Writing> writingList = new ArrayList<Writing>();
		SqlSession session = db.getSession();
		HashMap param = new HashMap();
		param.put("keyword", content);
		param.put("bNumber", bNumber);
		try {
			writingList = (ArrayList) session.selectList(
					"dbConfig.WritingMapper.contentSearch", param);
		} finally {
			if (session != null)
				session.close();
		}

		return writingList;
	}

	@Override
	public int getMaxwNumber() {
		// TODO Auto-generated method stub
		int number = 0;
		SqlSession session = db.getSession();
		try {
			Integer temp = session.selectOne("dbConfig.WritingMapper.maxwNumber");
			if(temp!=null)number=temp;
		} finally {
			if (session != null)
				session.close();
		}
		return number;
	}

	@Override
	public void hitsUp(int wNumber) {
		// TODO Auto-generated method stub
		SqlSession session = db.getSession();
		try {
			session.update("dbConfig.WritingMapper.hitsUp",wNumber);
			session.commit();
		} finally {
			if (session != null)
				session.close();
		}
	}

	@Override
	public int getListCount(int bNumber) {
		// TODO Auto-generated method stub
		int listCount=0;
		SqlSession session = db.getSession();
		
		try {
			listCount=session.selectOne("dbConfig.WritingMapper.listCount",bNumber);
			session.commit();
		} finally {
			if (session != null)
				session.close();
		}
		return listCount;
	}

	@Override
	public ArrayList<Writing> getPageWritings(int bNumber, int page, int limit) {
		// TODO Auto-generated method stub
		ArrayList<Writing> writingList=getAllWritings(bNumber);
		int startRow=(page-1)*10;
		int endRow=startRow+limit;
		
		if(endRow>writingList.size()){
			endRow=writingList.size();
		}
	
		ArrayList<Writing> pageList=new ArrayList<Writing>();
		
		
		pageList.addAll(writingList.subList(startRow, endRow));
		
	
		return pageList;
	}
	
	public  ArrayList<Writing> getSearchWriting(int bNumber, int page, int limit,String searchWord,int index){
		ArrayList<Writing> writingList=null;
		
		if(index==1){
			writingList=custNameSearch(searchWord,bNumber);
		}else if(index==2){
			writingList=titleSearch(searchWord,bNumber);
		}else if(index==3){
			writingList=contentSearch(searchWord,bNumber);
		}
		
		System.out.println("writingList");
		
		int startRow=(page-1)*10;
		int endRow=startRow+limit;
		
		if(endRow>writingList.size()){
			endRow=writingList.size();
		}
	
		ArrayList<Writing> pageList=new ArrayList<Writing>();
		
		
		pageList.addAll(writingList.subList(startRow, endRow));
		
	
		return pageList;
		
		
	}
	


}
